<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<body>
    <h1>Hello world!</h1>

    <button onclick="test1()">application/vnd.ms-excel 文件没有后缀</button>
    <button onclick="test2()">application/vnd.ms-excel 文件有后缀</button>
    <button onclick="test3()">application/octet-stream 文件没有后缀</button>
    <button onclick="test4()">application/octet-stream 文件有后缀</button>
    <button onclick="test5()">两者都有 文件没有后缀</button>
    <button onclick="test6()">两者都有 文件有后缀</button>

    <script th:inline="javascript">
        function test1() {
            $.ajax({
                async : false,
                cache : false,
                timeout: 1000,
                url: '/test1',
                type: "post",
                success: function(data){
                    const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
                    const link = document.createElement('a')
                    link.download = '测试下载'
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    document.body.removeChild(link)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }

         function test2() {
            $.ajax({
                async : false,
                cache : false,
                timeout: 1000,
                url: '/test1',
                type: "post",
                success: function(data){
                    const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
                    const link = document.createElement('a')
                    link.download = '测试下载.xlsx'
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    document.body.removeChild(link)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }

          function test3() {
            $.ajax({
                async : false,
                cache : false,
                timeout: 1000,
                url: '/test2',
                type: "post",
                success: function(data){
                    const blob = new Blob([data], { type: 'application/octet-stream' })
                    const link = document.createElement('a')
                    link.download = '测试下载'
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    document.body.removeChild(link)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }

        function test4() {
            $.ajax({
                async : false,
                cache : false,
                timeout: 1000,
                url: '/test2',
                type: "post",
                success: function(data){
                    const blob = new Blob([data], { type: 'application/octet-stream' })
                    const link = document.createElement('a')
                    link.download = '测试下载.xlsx'
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    document.body.removeChild(link)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }

        function test5() {
            $.ajax({
                async : false,
                cache : false,
                timeout: 1000,
                url: '/test3',
                type: "post",
                success: function(data){
                    const blob = new Blob([data], { type: 'application/octet-stream' })
                    const link = document.createElement('a')
                    link.download = '测试下载'
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    document.body.removeChild(link)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }

        function test6() {
            $.ajax({
                async : false,
                cache : false,
                timeout: 1000,
                url: '/test3',
                type: "post",
                success: function(data){
                    const blob = new Blob([data], { type: 'application/octet-stream' })
                    const link = document.createElement('a')
                    link.download = '测试下载.xlsx'
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob)
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    document.body.removeChild(link)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }

    </script>
</body>
</html>